home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / minrec.zip / MINREC.C < prev    next >
C/C++ Source or Header  |  1993-04-30  |  10KB  |  281 lines

  1. // ************************************************************************
  2. //
  3. //                      Microsoft Developer Support
  4. //            Copyright (c) 1992, 1993 Microsoft Corporation
  5. //
  6. // **************************************************************************
  7. // MODULE    : MinRec.C
  8. // PURPOSE   : A Small Win32 Dialog Sample Application Template
  9. // FUNCTIONS :
  10. //   WinMain()         - initializes the main window, dispatches messages
  11. //   MainDlgProc()     - processes messages for the main dialog box window
  12. //   ErrorMessageBox() - displays an error message box when called
  13. // **************************************************************************
  14. #define   STRICT               // strict type checking enabled
  15. #include <Windows.H>           // required for all Windows applications
  16.  
  17. #include "RecHook.H"
  18. #include "MinRec.H"           // specific to this program
  19.  
  20. //-- global data
  21. HWND      hDlgMain;            // main dialog handle
  22. HINSTANCE hInstance;           // current instance
  23.  
  24. //-- internal function prototypes
  25. BOOL    CALLBACK MainDlgProc  ( HWND, UINT, WPARAM, LPARAM );
  26. BOOL    CALLBACK AboutDlgProc ( HWND, UINT, WPARAM, LPARAM );
  27. BOOL             ErrorMessageBox ( LPCTSTR, LPCTSTR, LPCTSTR, INT );
  28.  
  29. // **************************************************************************
  30. // FUNCTION : WinMain( HINSTANCE, HINSTANCE, LPSTR, INT )
  31. // PURPOSE  : Calls initialization function, processes message loop
  32. // COMMENTS :
  33. // **************************************************************************
  34. INT PASCAL
  35. WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, INT nCmdShow )
  36. {
  37.   MSG     Msg;
  38.   HACCEL  hAccel;
  39.   LPCTSTR lpszClassName   = TEXT( "MinRecClass" );
  40.   LPCTSTR lpszMenuName    = TEXT( "MinRecMenu"  );
  41.   LPCTSTR lpszIconName    = TEXT( "MinRecIcon"  );
  42.   LPCTSTR lpszAccelName   = TEXT( "MinRecAccel" );
  43.  
  44.   //-- check if Win32s, if so display notice and terminate
  45.   if( GetVersion() & 0x80000000 ) {
  46.     MessageBox( NULL,
  47.       TEXT( "Sorry, this Sample Win32 application\n" )
  48.       TEXT( "requires Windows NT.\n"                 )
  49.       TEXT( "This application will now terminate."   ),
  50.       TEXT( "Windows NT Required!"                   ),
  51.       MB_OK );
  52.     return( -1 );
  53.   }
  54.  
  55.   hInstance = hInst;
  56.  
  57.   //-- Other instances of app running? If not...
  58.   if( !hPrevInst ) {
  59.     WNDCLASS WndClass;
  60.  
  61.     WndClass.style         = CS_HREDRAW | CS_VREDRAW;
  62.     WndClass.lpfnWndProc   = (WNDPROC) MainDlgProc;
  63.     WndClass.cbClsExtra    = (int) NULL;
  64.     WndClass.cbWndExtra    = DLGWINDOWEXTRA;
  65.     WndClass.hInstance     = hInstance;
  66.     WndClass.hIcon         = LoadIcon( hInstance, lpszIconName );
  67.     WndClass.hCursor       = LoadCursor( NULL, IDC_ARROW );
  68.     WndClass.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE+1);
  69.     WndClass.lpszMenuName  = lpszMenuName;
  70.     WndClass.lpszClassName = lpszClassName;
  71.  
  72.     if( !RegisterClass(&WndClass) ) {
  73.       ErrorMessageBox( "Failed!", "RegisterClass()", __FILE__, __LINE__ );
  74.       return( FALSE );
  75.     }
  76.   }
  77.  
  78.   //-- Create a main dialog window for this application instance
  79.   hDlgMain = CreateDialog( hInstance, "MainDlgBox", NULL, NULL );
  80.  
  81.   //-- If window could not be created, return "failure"
  82.   if( !hDlgMain ) {
  83.     ErrorMessageBox( "Failed!", "CreateDialog()", __FILE__, __LINE__ );
  84.     return( FALSE );
  85.   }
  86.  
  87.   //-- Load main menu accelerators
  88.   if( !(hAccel = LoadAccelerators( hInstance, lpszAccelName) ) ) {
  89.     ErrorMessageBox( "Failed!", "LoadAccelerators()", __FILE__, __LINE__ );
  90.     return( FALSE );
  91.   }
  92.  
  93.   //-- Make the window visible; update its client area; and return "success"
  94.   ShowWindow( hDlgMain, SW_SHOWDEFAULT );  // Show the dialog
  95.  
  96.   //-- Acquire and dispatch messages until a WM_QUIT message is received.
  97.   while( GetMessage( &Msg, NULL, 0, 0 ) ) {
  98.     if( !TranslateAccelerator( hDlgMain, hAccel, &Msg ) ) {
  99.       if( !IsDialogMessage( hDlgMain, &Msg ) ) {
  100.         TranslateMessage( &Msg );     // Translates virtual key codes
  101.         DispatchMessage( &Msg );      // Dispatches message to window
  102.       }
  103.     }
  104.   }
  105.  
  106.   return( Msg.wParam );           // Returns the value from PostQuitMessage
  107.   UNREFERENCED_PARAMETER( lpCmdLine );  // avoid the warning
  108. }
  109.  
  110.  
  111. // **************************************************************************
  112. // FUNCTION : MainDlgProc( HWND, UINT, WPARAM, LPARAM )
  113. // PURPOSE  : Processes messages
  114. // MESSAGES :
  115. //   WM_COMMAND         - application menu
  116. //     IDM_FILE_EXIT    - exit the application
  117. //     IDM_HELP_ABOUT   - About Dialog Box
  118. //     ...
  119. //   WM_CREATE          - window initialization
  120. //   WM_CLOSE           - handles cleanup
  121. //   WM_DESTROY         - destroys window
  122. // **************************************************************************
  123. BOOL CALLBACK
  124. MainDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  125. {
  126.   switch( uMsg ) {
  127.  
  128.     case WM_COMMAND:
  129.       switch( LOWORD(wParam) ) {
  130.  
  131.         case IDM_FILE_EXIT:
  132.           PostMessage( hWnd, WM_CLOSE, (WPARAM) 0, (LPARAM) 0 );
  133.           return( TRUE );
  134.  
  135.         case IDM_RECORD_START:
  136.           StartJournalRecord();
  137.           return( TRUE );
  138.  
  139.         case IDM_RECORD_STOP:
  140.           StopJournalRecord();
  141.           return( TRUE );
  142.  
  143.         case IDM_PLAYBACK:
  144.           StartJournalPlayback();
  145.           return( TRUE );
  146.  
  147.         case IDM_HELP_ABOUT: {
  148.           DLGPROC lpfnAboutDlgProc;  // pointer to the "About" function
  149.           LPSTR   lpszAboutSample = "AboutDlgBox";
  150.  
  151.           lpfnAboutDlgProc = (DLGPROC) MakeProcInstance( (FARPROC) AboutDlgProc, hInstance );
  152.           if( DialogBox( hInstance, lpszAboutSample, hWnd, lpfnAboutDlgProc ) == -1)
  153.             ErrorMessageBox( "Failed!", "DialogBox()", __FILE__, __LINE__ );
  154.           FreeProcInstance( (FARPROC) lpfnAboutDlgProc );
  155.           return( TRUE );
  156.         }
  157.  
  158.       }
  159.       break;
  160.  
  161.     case WM_INITDIALOG:
  162.       return( TRUE );
  163.  
  164.     case WM_CLOSE:
  165.       DestroyWindow( hDlgMain ); hDlgMain = NULL;
  166.       PostQuitMessage( FALSE );
  167.   }
  168.  
  169.   return( DefDlgProc( hWnd, uMsg, wParam, lParam )  );
  170. }
  171.  
  172.  
  173. // **************************************************************************
  174. // FUNCTION : AboutDlgProc( HWND, UINT, WPARAM, LPARAM )
  175. // PURPOSE  : Processes messages for "About" dialog box
  176. // MESSAGES :
  177. //   WM_INITDIALOG - initialize dialog box
  178. //   WM_COMMAND    - Input received
  179. //     IDOK        - OK button selected
  180. //     IDCANCEL    - Cancel button selected
  181. // COMMENTS:
  182. //   No initialization is needed for this particular dialog box.
  183. //   In this case, TRUE must be returned to Windows.
  184. //   Wait for user to click on "Ok" button, then close the dialog box.
  185. // **************************************************************************
  186. BOOL CALLBACK
  187. AboutDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  188. {
  189.   switch( uMsg ) {
  190.  
  191.     case WM_COMMAND:
  192.       switch( LOWORD(wParam) ) {
  193.  
  194.         case IDOK:
  195.           EndDialog( hDlg, TRUE );
  196.           return( TRUE );
  197.  
  198.         case IDCANCEL:
  199.           EndDialog( hDlg, FALSE );
  200.           return( TRUE );
  201.       }
  202.       break;
  203.  
  204.     case WM_INITDIALOG:
  205.       return( TRUE );
  206.  
  207.     case WM_CLOSE:
  208.       return( TRUE );
  209.  
  210.   }
  211.  
  212.   return( FALSE );
  213.   UNREFERENCED_PARAMETER( lParam );
  214. }
  215.  
  216.  
  217. // ************************************************************************
  218. // FUNCTION : ErrorMessageBox( LPCTSTR, LPCTSTR, LPCTSTR, INT )
  219. // PURPOSE  : Displays an error message box with various error information
  220. //            and allows the user to terminate or continue the process.
  221. //            For a Win32 Application, GetLastError and FormatMessage are
  222. //            user to retrieve the last API error code and error message.
  223. // COMMENTS :
  224. // ************************************************************************
  225. BOOL
  226. ErrorMessageBox( LPCTSTR lpszText, LPCTSTR lpszTitle, LPCTSTR lpszFile,
  227.   INT Line )
  228. {
  229.   #define ERROR_BUFFER_SIZE 512
  230.  
  231.   static TCHAR Format[] =
  232.     TEXT( "%s\n\n"                                  )
  233.     TEXT( "-- Error Information --\n"               )
  234.     TEXT( "File : %s\n"                             )
  235.     TEXT( "Line : %d\n"                             )
  236.     TEXT( "Error Number : %d\n"                     )
  237.     TEXT( "Error Message : %s\n"                    )
  238.     TEXT( "\n"                                      )
  239.     TEXT( "Press OK to terminate this application." );
  240.  
  241.   LPTSTR lpFormatMessageBuffer;
  242.   DWORD  dwFormatMessage;
  243.   DWORD  dwGetLastError;
  244.   HLOCAL hMessageBoxBuffer;
  245.   LPVOID lpMessageBoxBuffer;
  246.  
  247.   //-- perform a simple check on the needed buffer size
  248.   if( lstrlen(lpszText) > (ERROR_BUFFER_SIZE - lstrlen(Format)) )
  249.     return( FALSE );
  250.  
  251.   //-- allocate the message box buffer
  252.   hMessageBoxBuffer  = LocalAlloc( LMEM_FIXED, ERROR_BUFFER_SIZE );
  253.   lpMessageBoxBuffer = LocalLock( hMessageBoxBuffer );
  254.  
  255.   //-- get the system error and system error message
  256.   dwGetLastError = GetLastError();
  257.   dwFormatMessage = FormatMessage(
  258.                       FORMAT_MESSAGE_ALLOCATE_BUFFER
  259.                       | FORMAT_MESSAGE_FROM_SYSTEM,
  260.                       NULL, dwGetLastError, LANG_NEUTRAL,
  261.                       (LPTSTR) &lpFormatMessageBuffer, 0, NULL );
  262.   if( !dwFormatMessage )
  263.     lpFormatMessageBuffer = TEXT("FormatMessage() failed!");
  264.  
  265.   //-- format the error messge box string
  266.   wsprintf( lpMessageBoxBuffer, Format, lpszText, lpszFile, Line,
  267.     dwGetLastError, lpFormatMessageBuffer );
  268.  
  269.   // -- display the error and allow the user to terminate or continue
  270.   if( MessageBox( NULL, lpMessageBoxBuffer, lpszTitle,
  271.         MB_APPLMODAL | MB_ICONSTOP | MB_OKCANCEL ) == IDOK )
  272.     ExitProcess( 0 );
  273.  
  274.   //-- free all buffers
  275.   if( dwFormatMessage )
  276.     LocalFree( (HLOCAL) lpFormatMessageBuffer );
  277.   LocalFree( (HLOCAL) hMessageBoxBuffer );
  278.  
  279.   return( TRUE );
  280. }
  281.